
package de.swta.freezay.database.wicketAdapter;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Reservation;
import org.apache.wicket.model.LoadableDetachableModel;

/**
 * A LoadableDetachableModel for reservation entities
 *
 * @author Christian Baeumer
 * @see LoadableDetachableModel
 */
public class DetachableReservationModel extends LoadableDetachableModel<Reservation> {

    /** the reservation number for this reservation model */
    private Integer reservationNr;

    public DetachableReservationModel(final Reservation r) {
        this(r.getReservationNr());
    }

    public DetachableReservationModel(final Integer reservationNr) {

        if (reservationNr == null) {
            throw new IllegalArgumentException("No reservation number " +
                    "specified");
        }

        this.reservationNr = reservationNr;
    }

    @Override
    protected Reservation load() {
        /** loads Reservation from database */
        return JPAController.ReservationJpaController.findReservation(reservationNr);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + (reservationNr != null
                ? reservationNr.hashCode() : 0);
        return hash;
    }

    /**
     * used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     *
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else if (obj instanceof DetachableReservationModel) {
            DetachableReservationModel other = (DetachableReservationModel) obj;
            return other.reservationNr.equals(reservationNr);
        }

        return false;
    }

}
